var params = {};
var defaultPageSize = 100000;
var currentPage = 1;
var pageSize = defaultPageSize;

var current = {};
var option = {};

$(function(){
	loadGrid();
	$('.check').on('click',search);
});

function search(){
	var keywords = $('#keywords').val();
	if(keywords!='')
	{
		//params.contractNum = keywords;
		if($('#contractHeaderId').val()==""){
			alert("请选择合同编号!");
		}else{
			var id=$('#contractHeaderId').val();
			load(id);
		}
	}
}

function load(id){
	//合同头==================================================
	$.ajax({
		url:webUrl+'contractHeader/getHeaderLineById.do',
		type:'post',
		data:{id:id}
	}).done(function(data){
		var d = data.bo;
		
		//alert(JSON.stringify(d.chwContractHeader.contractNum));
		if(d.chwContractHeader.contractNum!=null){
			$("#contractNum1").val(d.chwContractHeader.contractNum);
		}
		if(d.chwContractHeader.paymentTermId!=null){
			$("#paymentTermName").val(d.chwContractHeader.paymentTermId);
		}
		if(d.chwContractHeader.customerId!=null){
			$("#customerName").val(d.chwContractHeader.customerName);
		}
		if(d.chwContractHeader.signingDate!=null){
			$('#signingDate').datebox('setValue',formatTime(d.chwContractHeader.signingDate));
		}
		if(d.chwContractHeader.completionDate!=null){
			$('#completionDate').datebox('setValue',formatTime(d.chwContractHeader.completionDate));
		}
	
	//合同头==================================================
		//合同行==================================================
		$('#lineGrid').datagrid({
			fitColumns:true,
			singleSelect:true,
			pagination:true,
			remoteSort:false,
			multiSort:true,
			loadMsg:"数据加载中，请稍后……",
			data:d.chwContractLine,
			columns:[[
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'quantity',title:'数量',align:'center',width:150},
			          {field:'totalAmount',title:'金额',align:'center',width:150},
			      ]],
			      onSortColumn: function (sort, order) {
			    	  setPager(pager,result.total);
         }
		});
		var pager = $('#lineGrid').datagrid().datagrid('getPager');	// get the pager of datagrid
		setPager(pager,result.total);
		//合同行==================================================
		
	});
}

function setPager(pager,total){
	pager.pagination({
		total:total,
		pageNumber:currentPage,
	    pageSize:defaultPageSize,
	    layout:['first','prev','links','next','last'],
	    onSelectPage:function(cpage, psize){
	    	currentPage=cpage;
	    	pageSize=psize;
	    	load($('#contractHeaderId').val());
		}
	});
}

function loadGrid(){
	//alert(webUrl);
	params.currentPage = currentPage;
	params.pageSize = pageSize;
	$.ajax({
		url: webUrl+'contractHeader/findChwHeader.do',
		type:'post',
		data:params
	}).done(function(data){
		var result = data.bo;
		var availableTags = [];
		if(result.list.length>0){
			for(var i=0;i<result.list.length;i++){
				//alert(JSON.stringify(result.list[i].contractNum));
				availableTags[i]={value:result.list[i].contractNum,key: result.list[i].contractHeaderId};
			}
		}
		$('#contractNum').combobox({
	         valueField: 'key', //TPrice
	         textField: 'value',
	         data:availableTags,
	         //注册事件
            onChange: function (newValue, oldValue) {
           	 var bool=false;
           	 for(var k=0 ;k<availableTags.length;k++){
           		 if(newValue==availableTags[k].value){
           			 bool=false;
           			 break;
           		 }else{
           			 bool=true;
           		 }
           	 }
           	 if(bool){
           		 $('#contractHeaderId').val("");
           	 }
            },
	         onSelect: function (record) {
	        	 $('#contractHeaderId').val(record.key);
	        	 $('#contractNum').combobox('setValue', record.value);
	         }
	     });
	});
}

var formatId = function(val, row,index){
	return (index+1)+(currentPage-1)*pageSize;
}


var formatTime = function(val){
	if (val != null) {
		var date = new Date(val);
		return date.getFullYear() + '-' + (date.getMonth() + 1) + '-'+ date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
	}
}
